const axios = require('axios');

const API_BASE = 'http://localhost:3000';

async function testChatAPI() {
  console.log('开始测试聊天API...\n');
  
  try {
    // 测试创建与医院的聊天会话
    console.log('1. 测试创建与医院的聊天会话');
    const createChatRes = await axios.post(`${API_BASE}/chats/openOrCreate`, {
      fromUserId: '68aeed055e85b6f7f7ff0480',
      targetType: 'hospital',
      targetId: 'hospital_001',
      targetName: '北京医美医院'
    });
    
    console.log('创建会话成功:', createChatRes.data);
    const chatId = createChatRes.data._id;
    
    // 测试发送消息
    console.log('\n2. 测试发送消息');
    const sendMessageRes = await axios.post(`${API_BASE}/chats/${chatId}/messages`, {
      senderId: '68aeed055e85b6f7f7ff0480',
      content: '你好，我想咨询一下医美项目',
      type: 'text'
    });
    
    console.log('发送消息成功:', sendMessageRes.data);
    
    // 测试获取消息列表
    console.log('\n3. 测试获取消息列表');
    const getMessagesRes = await axios.get(`${API_BASE}/chats/${chatId}/messages`);
    
    console.log('获取消息成功:', getMessagesRes.data);
    
    // 测试发送医院回复
    console.log('\n4. 测试发送医院回复');
    const hospitalReplyRes = await axios.post(`${API_BASE}/chats/${chatId}/messages`, {
      senderId: 'hospital_hospital_001',
      content: '您好！欢迎咨询我们的医美项目，请问您想了解哪个方面？',
      type: 'text'
    });
    
    console.log('医院回复成功:', hospitalReplyRes.data);
    
    // 再次获取消息列表
    console.log('\n5. 再次获取消息列表');
    const getMessagesRes2 = await axios.get(`${API_BASE}/chats/${chatId}/messages`);
    
    console.log('最终消息列表:', getMessagesRes2.data);
    
    console.log('\n✅ 聊天API测试完成！');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.response?.data || error.message);
  }
}

// 运行测试
testChatAPI();
